package com.dkd.easybook.mapper;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class JsonListTypeHandler extends BaseTypeHandler<List<?>> {

    private final ObjectMapper objectMapper = new ObjectMapper();

    // 确保有无参构造函数
    public JsonListTypeHandler() {
        super();
    }

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, List<?> parameter, JdbcType jdbcType) throws SQLException {
        try {
            ps.setString(i, objectMapper.writeValueAsString(parameter));
        } catch (Exception e) {
            throw new SQLException("Error converting list to JSON string", e);
        }
    }

    @Override
    public List<?> getNullableResult(ResultSet rs, String columnName) throws SQLException {
        try {
            String json = rs.getString(columnName);
            if (json == null) {
                return null;
            }
            // 尝试解析为 List<String>
            try {
                return objectMapper.readValue(json, new TypeReference<List<String>>() {});
            } catch (Exception e) {
                // 如果解析失败，尝试解析为 List<Map<String, Object>>
                return objectMapper.readValue(json, new TypeReference<List<Map<String, Object>>>() {});
            }
        } catch (Exception e) {
            throw new SQLException("Error converting JSON string to list", e);
        }
    }

    @Override
    public List<?> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        try {
            String json = rs.getString(columnIndex);
            if (json == null) {
                return null;
            }
            // 尝试解析为 List<String>
            try {
                return objectMapper.readValue(json, new TypeReference<List<String>>() {});
            } catch (Exception e) {
                // 如果解析失败，尝试解析为 List<Map<String, Object>>
                return objectMapper.readValue(json, new TypeReference<List<Map<String, Object>>>() {});
            }
        } catch (Exception e) {
            throw new SQLException("Error converting JSON string to list", e);
        }
    }

    @Override
    public List<?> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        try {
            String json = cs.getString(columnIndex);
            if (json == null) {
                return null;
            }
            // 尝试解析为 List<String>
            try {
                return objectMapper.readValue(json, new TypeReference<List<String>>() {});
            } catch (Exception e) {
                // 如果解析失败，尝试解析为 List<Map<String, Object>>
                return objectMapper.readValue(json, new TypeReference<List<Map<String, Object>>>() {});
            }
        } catch (Exception e) {
            throw new SQLException("Error converting JSON string to list", e);
        }
    }
}
